package com.springboot.astra.controller;

import com.springboot.astra.myEcxeption.TokenException;
import com.springboot.astra.result.DataResult;
import org.apache.log4j.Logger;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
public class GlobalExceptionHandler {

    static final Logger logger = Logger.getLogger(GlobalExceptionHandler.class);

    @ExceptionHandler(value = NullPointerException.class)
    public DataResult exceptionHandler(NullPointerException e){
        logger.error("发生空指针异常！原因是:",e);
        return DataResult.failWithMsg(e.getMessage());
    }

    @ExceptionHandler(value = Exception.class)
    public DataResult unKnowException(Exception e){
        logger.error("发生未知错误，原因是：",e);
        return DataResult.failWithMsg("服务器发生未知错误，请联系相关维护人员");
    }

    @ExceptionHandler(value = TokenException.class)
    public DataResult tokenExceptionHandler(TokenException e){
        logger.error("信息验证失败，原因是："+e.getMessage());
        return DataResult.failWithMsg(e.getMessage());
    }

}
